package com.huluobo.a2303mdmdemo.demo

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.ViewModelProvider
import com.huluobo.a2303mdmdemo.MainActivity
import kotlinx.coroutines.runBlocking

/**
 *  Created by LC on 2025/9/2.
 */
class MyView(context: Context) : View(context) {

//    //kotlin中的多种构造写法
//    constructor(context: Context) : super(context)
//
//    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
//
//    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
//        context,
//        attributeSet,
//        defStyleAttr
//    )
//
//    //测量
//    //widthMeasureSpec 包含2个值 是一个32位的数字 前2位代表当前的测量模式(mode),后30位代表具体大小(size)
//    // 1.UNSPECIFIED 系统管理,一般用于列表计算长度,比如RecyclerView或者ScrollView
//    // 2.EXACTLY 实际的值,一般用于MatchParent或者40.dp 60.dp 这些具体的值
//    // 3.AT_MOST 不确定大小的值,根据内容的变化产生实际的值,一眼用于warpContent
//    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//    }

    //布局摆放 一般单独的View不会使用这个方法,只有在Layout中才会使用,比如LinearLayout
    //流式布局的布局方式就是计算ltrb的方式
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    //画笔
    private lateinit var paint: Paint
    private lateinit var paintText: Paint

    //初始化 kotlin提供了类初始化的位置,Java中可以直接写到构造方法里,构造方法就是Java的初始化位置
    init {
        initPaint()
        start()
    }

    private fun initPaint() {
        paint = Paint()
        paintText = Paint()
        paint.color = Color.WHITE
        paintText.textSize = 100f
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 5f
        paintText.color = Color.WHITE
    }

    //绘制的角度
    private var progress = 0f
    private var num = 5

    //1.声明接口
    interface OnTimeEndListener {
        fun onTimeEnd()
    }

    //2.当前类中声明接口对象
    private lateinit var onTimeEndListener: OnTimeEndListener

    //3.给接口一个set方法
    fun setOnTimeEndListener(onTimeEndListener: OnTimeEndListener) {
        this.onTimeEndListener = onTimeEndListener
    }


    //绘制
    //在onDraw中所有的尺寸都是px
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        canvas.drawText("123", 100f, 100f, paint)

//        canvas.drawRect(100f, 100f, 600f, 600f, paint)
//        canvas.drawRect(0f, 0f, 1070f, 1070f, paint)
        //绘制一个矩形/范围
        val rectF = RectF(100f, 100f, 300f, 300f)
//        canvas.drawRect(rectF, paint)

        canvas.drawArc(rectF, 270f, progress, false, paint)
        canvas.drawText(num.toString(), 175f, 235f, paintText)
//        canvas.drawArc(100f, 100f, 600f, 600f, 0f, 360f, true, paint)
    }

    fun start() {
        //协程中的new Thread 不需要管理生命周期,直接使用即可
        runBlocking {
            val animator = ValueAnimator.ofFloat(0f, 360f)
            animator.addUpdateListener {
                //把动画中的值赋值给绘制的角度
                progress = it.animatedValue as Float
                //刷新自身
                invalidate()
            }
            animator.duration = 5000
            //使用线性插值器(匀速插值器)能让动画展示的更加平滑
            animator.interpolator = LinearInterpolator()
            animator.start()

            val animatorText = ValueAnimator.ofInt(5, 0)
            animatorText.addUpdateListener {
                num = it.animatedValue as Int
                invalidate()
                if (num == 0) {
                    //4.对应的位置调用接口中的方法
                    onTimeEndListener.onTimeEnd()
                }
            }
            animatorText.interpolator = LinearInterpolator()
            animatorText.duration = 5000
            animatorText.start()
        }
    }
}